﻿using System;

using Microsoft.MSE.Catalog.SelfHost;
using System.Diagnostics;

namespace Microsoft.MSE.Catalog.ConsoleHost
{
    /// <summary>
    /// Console implementation of self-host container.
    /// </summary>
    internal static class Program
    {
        /// <summary>
        /// Generic implementation of self-host container for use in console or Windows service.
        /// </summary>
        private static CatalogServer _customHost;

        /// <summary>
        /// Entry point for console host of MSE Catalog Server.
        /// </summary>
        /// <param name="args">Command line arguments.</param>
        private static void Main(string[] args)
        {
            // startup information should clearly show the following:
            //
            // 1) identity that the catalog server is running under
            // 2) sql instance
            // 3) sql database
            // 4) exposed services

            try
            {
                TraceHelper.InitializeTraceSource("mseTraceSrc");
                ConsoleTraceListener ctl = new ConsoleTraceListener(true);
                ctl.Writer = Console.Out;
                TraceHelper.MseTraceSource.Listeners.Add(ctl);

                _customHost = new CatalogServer(args);

                Console.Title = _customHost.Name;
                Console.WriteLine(_customHost.Header);

                if (_customHost.ErrorMessages.Count > 0)
                {
                    Console.ForegroundColor = ConsoleColor.Red;

                    foreach (string message in _customHost.ErrorMessages)
                    {    
                        Console.WriteLine(message);
                        Console.WriteLine();
                    }

                    Console.ResetColor();
                    Console.WriteLine(_customHost.Usage);
                    return;
                }

                if (_customHost.WarningMessages.Count > 0)
                {
                    Console.ForegroundColor = ConsoleColor.Yellow;

                    foreach (string message in _customHost.WarningMessages)
                    {
                        Console.WriteLine(message);
                        Console.WriteLine();
                    }

                    Console.ResetColor();
                }

                foreach (string message in _customHost.InformationMessages)
                {
                    Console.WriteLine(message);
                }

                _customHost.Start();
            }
            catch (Exception ex)
            {
                Console.ForegroundColor = ConsoleColor.Red;
                Console.WriteLine(ex);
                Console.ResetColor();
            }
            finally
            {
                Console.WriteLine("Press Enter to Exit");
                Console.ReadLine();

                if (_customHost != null)
                {
                    _customHost.Stop();
                }
            }
        }
    }
}